package com.edooteach.api.web.controller.auth;

import java.util.Map;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.demo2do.core.entity.result.HttpResult;
import com.edoomath.entity.user.User;
import com.edooteach.api.handler.SmsSender;
import com.edooteach.api.servcice.UserService;

/**
 * 
 * @author haiyan_xiao
 *
 */
@RestController
@RequestMapping("")
public class LoginController {

    private static final Logger logger = LoggerFactory.getLogger(LoginController.class);

    @Autowired
    private SmsSender smsSender;

    @Autowired
    private UserService userService;

    /**
     * 
     * @param unionid
     * @param mobile
     * @param code
     * @param token
     * @param openid
     * @return
     */
    @RequestMapping(value = "/login", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE, params = "unionid")
    public HttpResult onMobileLogin(@RequestParam("unionid") String unionid,
                                    @RequestParam("mobile") String mobile,
                                    @RequestParam("code") String code,
                                    @RequestParam("token") String token,
                                    @RequestParam("openid") String openid) {
        HttpResult httpResult = new HttpResult();
        
        try {
            
            logger.info("verify code mobile [{}] token [{}] code [{}]", mobile, token, code);
            if(StringUtils.isNotBlank(unionid) && StringUtils.isNotBlank(mobile) && StringUtils.isNotBlank(openid)) {
                mobile = mobile.replace(" ", "");
                
                boolean valid = smsSender.checkVerifyCode(mobile, code, token);
                
                if(valid) {
                    logger.info("LoginController#onMobileLogin login with mobile [{}] openid [{}] unionid [{}]", mobile, openid, unionid);
                    
                    User user = userService.login(mobile, openid, unionid);
                    httpResult.success().message("绑定成功").data("user", user);
                    
                } else {
                    httpResult.fail("1").message("验证码不正确");
                }
            } else {
                httpResult.fail("1").message("请求参数有误");
            }
        } catch (Exception e) {
            logger.error("LoginController#onMobileLogin error", e);
            httpResult.fail("500").message("绑定手机号失败");
        }
        
        return httpResult;
    }

    /**
     * 
     * @param unionid
     * @return
     */
    @RequestMapping(value = "/get-mobile", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE, params = "unionid")
    public HttpResult getMobile(@RequestParam("unionid") String unionid,
                                @RequestBody Map<String, String> params) {
        HttpResult httpResult = new HttpResult();
        
        try {
            User user = userService.load(unionid);
            
            if(user == null) {
                httpResult.success().message("查询成功");
            } else {
                httpResult.success().message("查询成功").data("mobile", user.getMobile());
            }
        } catch (Exception e) {
            logger.error("LoginController#getMobile error", e);
            httpResult.fail("500").message("查询失败");
        }
        
        return httpResult;
    }
}
